from operator import itemgetter
import plotly.express as px
import requests

# 执行API调用并查看响应
url = "https://hacker-news.firebaseio.com/v0/topstories.json"
r = requests.get(url)
print(f"Status code:{r.status_code}")

# 处理有关每篇文章的信息
submission_ids = r.json()
submission_dicts = []
for submission_id in submission_ids[:20]:
    # 对于每篇文章，都执行一个API调用
    url = f"https://hacker-news.firebaseio.com/v0/item/{submission_id}.json"
    r = requests.get(url)
    print(f"id:{submission_id}\tstatus:{r.status_code}")
    response_dict = r.json()

    # 对于每篇文章，都创建一个字典
    submission_dict = {
        'title':response_dict['title'],
        'hn_link':f"https://news.ycombinator.com/item?id={submission_id}",
        'comments':response_dict['descendants'],
    }
    submission_dicts.append(submission_dict)

submission_dicts = sorted(submission_dicts,key=itemgetter('comments'),
                          reverse=True)

# 提取数据
links , hover_text , comments = [] , [] , []
for submission_dict in submission_dicts:

    title = submission_dict['title'][:30]
    a_url = submission_dict['hn_link']
    link = f'<a href={a_url}"">{title}</a>'

    links.append(link)
    hover_text .append(submission_dict['title'])
    comments.append(submission_dict['comments'])

# 根据评论数量绘图
title = 'Most Commented Articles on Hacker News'
label = {'x':'Articles','y':'Comments Count'}
fig = px.bar(
    x = links, y = comments, hover_name = hover_text,
    title = title, labels = label
)

fig.update_layout(title_font_size=28,xaxis_title_font_size = 20,
                  yaxis_title_font_size = 20)

fig.update_traces(marker_color='SteelBlue', marker_opacity=0.6)

fig.show()